package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import bean.Room;
import common.util.DBUtils;
import dao.RoomDao;

public class RoomDaoImpl implements RoomDao {

	@Override
	public List<Room> queryHotRoom() throws SQLException {

		//创建连接
		Connection conn = DBUtils.getConnection();
		
		//数据操作
		String sql = "select * from room where rstate = ?";
		PreparedStatement psmt = conn.prepareStatement(sql);
		psmt.setInt(1, 1);
		ResultSet rs = psmt.executeQuery();
		
		//遍历结果集
		List<Room> rooms = new ArrayList<>();
		while(rs.next()) {
			String roomid = rs.getString("roomid");
			String typeId = rs.getString("typeId");
			String rstate = rs.getString("rstate");
			String rremarks = rs.getString("rremarks");
			String rname = rs.getString("rname");
			String rImg = rs.getString("rImg");
			double rPrice = rs.getDouble("rPrice");
			Room room = new Room(roomid, typeId, rstate, rremarks,
					rname, rImg,rPrice);
			rooms.add(room);
		}
				
		//关闭资源
		DBUtils.closeResource(conn, psmt, rs);
		
		return rooms;
	}

	@Override
	public List<Room> queryByName(String name) throws SQLException {

		//创建连接
		Connection conn = DBUtils.getConnection();
		
		//数据操作
		String sql = "select * from room where rname like ?";
		PreparedStatement psmt = conn.prepareStatement(sql);
		//% sql通配符，表示任意多个（0个，1个，多个）任意字符
		//_ sql通配符，表示1个任意字符
		//如果查询内容包含通配符，处理方式1，使用sql转义字符 escape
		//处理方式2， 如果%，先将%替换成1个字符串“##”，模糊查询对象“##”，String字符串替换的方法
		psmt.setString(1, "%" + name + "%");
		ResultSet rs = psmt.executeQuery();
		
		//遍历结果集
		List<Room> rooms = new ArrayList<>();
		while(rs.next()) {
			String roomid = rs.getString("roomid");
			String typeId = rs.getString("typeId");
			String rstate = rs.getString("rstate");
			String rremarks = rs.getString("rremarks");
			String rname = rs.getString("rname");
			String rImg = rs.getString("rImg");
			double rPrice = rs.getDouble("rPrice");
			Room room = new Room(roomid, typeId, rstate, rremarks,
					rname, rImg,rPrice);
			rooms.add(room);
		}
		
		//关闭资源
		DBUtils.closeResource(conn, psmt, rs);
				
		return rooms;
	}

	@Override
	public List<Room> queryByType(String tid) throws SQLException {

		//创建连接
		Connection conn = DBUtils.getConnection();
		
		//数据操作
		String sql = "select * from room where typeid = ?";
		PreparedStatement psmt = conn.prepareStatement(sql);
		psmt.setString(1, tid);
		ResultSet rs = psmt.executeQuery();
		
		//遍历结果集
		List<Room> rooms = new ArrayList<>();
		while(rs.next()) {
			String roomid = rs.getString("roomId");
			String typeId = rs.getString("typeId");
			String rstate = rs.getString("rstate");
			String rremarks = rs.getString("rremarks");
			String rname = rs.getString("rname");
			String rImg = rs.getString("rImg");
			double rPrice = rs.getDouble("rPrice");
			Room room = new Room(roomid, typeId, rstate, rremarks,
					rname, rImg,rPrice);
			rooms.add(room);
		}
		//关闭资源
		DBUtils.closeResource(conn, psmt, rs);
				
		return rooms;
	}

	@Override
	public boolean insert(Room t) throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void deleteById(String id) throws SQLException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update(Room t) throws SQLException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Room queryByid(String id) throws SQLException {

		//创建连接
		Connection conn = DBUtils.getConnection();
		
		//数据操作
		String sql = "select * from room where roomid = ?";
		PreparedStatement psmt = conn.prepareStatement(sql);
		psmt.setString(1, id);
		ResultSet rs = psmt.executeQuery();
		
		//遍历结果集
		Room room = null;
		if(rs.next()) {
			String roomid = rs.getString("roomid");
			String typeId = rs.getString("typeId");
			String rstate = rs.getString("rstate");
			String rremarks = rs.getString("rremarks");
			String rname = rs.getString("rname");
			String rImg = rs.getString("rImg");
			double rPrice = rs.getDouble("rPrice");
			room = new Room(roomid, typeId, rstate, rremarks,
					rname, rImg,rPrice);
		}
				
		//关闭资源
		DBUtils.closeResource(conn, psmt, rs);
		
		return room;
	}

	@Override
	public List<Room> queryAll() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

}
